using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 接口IInspectUserInfo的实现
    /// </summary>
    public class InspectUserInfo : IDAL.IInspectUserInfo
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            string strSql = "select count(*) from InspectUserInfo where Enabled=1 and InspectUserID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            string strSql = "select count(*) from InspectUserInfo where Enabled=1 and InspectUserName = '" + name.Trim() + "'";
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 增加一条记录
        /// </summary>
        public bool Add(Model.InspectUserInfo model)
        {
            string strSql = "insert into InspectUserInfo(INSPECTUSERNAME, INSPECTUSERPASSWORD, INSPECTUSERSCOPE, ENABLED) values(@InspectUserName, @InspectUserPassword, @InspectUserScope, @Enabled)";
            SqlParameter InspectUserName = new SqlParameter("INSPECTUSERNAME", SqlDbType.NVarChar); InspectUserName.Value = model.InspectUserName;
            SqlParameter InspectUserPassword = new SqlParameter("INSPECTUSERPASSWORD", SqlDbType.NVarChar); InspectUserPassword.Value = model.InspectUserPassword;
            SqlParameter InspectUserScope = new SqlParameter("INSPECTUSERSCOPE", SqlDbType.NVarChar); InspectUserScope.Value = model.InspectUserScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { InspectUserName, InspectUserPassword, InspectUserScope, Enabled }) == 1 ? true : false;
        }

        /// <summary>
        /// 增加一条记录，返回ID
        /// </summary>
        public int Add2(Model.InspectUserInfo model)
        {
            string strSql = "insert into InspectUserInfo(INSPECTUSERNAME, INSPECTUSERPASSWORD, INSPECTUSERSCOPE, ENABLED) values(@InspectUserName, @InspectUserPassword, @InspectUserScope, @Enabled) SELECT @@IDENTITY AS returnName;";
            SqlParameter InspectUserName = new SqlParameter("INSPECTUSERNAME", SqlDbType.NVarChar); InspectUserName.Value = model.InspectUserName;
            SqlParameter InspectUserPassword = new SqlParameter("INSPECTUSERPASSWORD", SqlDbType.NVarChar); InspectUserPassword.Value = model.InspectUserPassword;
            SqlParameter InspectUserScope = new SqlParameter("INSPECTUSERSCOPE", SqlDbType.NVarChar); InspectUserScope.Value = model.InspectUserScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetInsertID(strSql, new SqlParameter[] { InspectUserName, InspectUserPassword, InspectUserScope, Enabled });
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        public bool Update(Model.InspectUserInfo model)
        {
            string strSql = "update InspectUserInfo set INSPECTUSERNAME = @InspectUserName, INSPECTUSERPASSWORD = @InspectUserPassword, INSPECTUSERSCOPE = @InspectUserScope, ENABLED = @Enabled where InspectUserID= " + model.InspectUserID.ToString();
            SqlParameter InspectUserName = new SqlParameter("INSPECTUSERNAME", SqlDbType.NVarChar); InspectUserName.Value = model.InspectUserName;
            SqlParameter InspectUserPassword = new SqlParameter("INSPECTUSERPASSWORD", SqlDbType.NVarChar); InspectUserPassword.Value = model.InspectUserPassword;
            SqlParameter InspectUserScope = new SqlParameter("INSPECTUSERSCOPE", SqlDbType.NVarChar); InspectUserScope.Value = model.InspectUserScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { InspectUserName, InspectUserPassword, InspectUserScope, Enabled }) == 1 ? true : false;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        public bool Delete(int Id)
        {
            string strSql = "delete from InspectUserInfo where InspectUserID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.InspectUserInfo GetModel(int Id)
        {
            string strSql = "select * from InspectUserInfo where InspectUserID = " + Id.ToString();
            Model.InspectUserInfo model = new Model.InspectUserInfo();
            DataSet ds = DBHelperSQL.GetDataSet(strSql);
            model.InspectUserID = Id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.InspectUserID = Convert.ToInt32(ds.Tables[0].Rows[0]["InspectUserID"]);
                model.InspectUserName = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserName"]);
                model.InspectUserPassword = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserPassword"]);
                model.InspectUserScope = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserScope"]);
                model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.InspectUserInfo GetModel(string name)
        {
            //string strSql = "select * from InspectUserInfo where InspectUserName = '" + name.Trim() + "'";
            //Model.InspectUserInfo model = new Model.InspectUserInfo();
            //DataSet ds = DBHelperSQL.GetDataSet(strSql);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
                //model.InspectUserID = Convert.ToInt32(ds.Tables[0].Rows[0]["InspectUserID"]);
                //model.InspectUserName = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserName"]);
                //model.InspectUserPassword = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserPassword"]);
                //model.InspectUserScope = Convert.ToString(ds.Tables[0].Rows[0]["InspectUserScope"]);
                //model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                //return model;
            //}
            //else
            //{
                //return null;
            //}
            return null;
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "select * from InspectUserInfo where Enabled=1 ";
            if (strWhere.Trim() != "")
                strSql += "and " + strWhere;
            return DBHelperSQL.GetDataSet(strSql);
        }
    }
}
